#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=hplip_lede
PKG_VERSION:=3.24.4
PKG_RELEASE:=1

PKG_SRC_NAME:=hplip
PKG_SOURCE:=$(PKG_SRC_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=@SF/hplip
PKG_HASH:=5d7643831893a5e2addf9d42d581a5dbfe5aaf023626886b8762c5645da0f1fb

PKG_MAINTAINER:=Luiz Angelo Daros de Luca <luizluca@gmail.com>
PKG_LICENSE:=GPL-2.0 GPL-2.0-or-later
PKG_LICENSE_FILES:=COPYING LICENSE

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_SRC_NAME)-$(PKG_VERSION)


PKG_BUILD_PARALLEL:=1
#PKG_BUILD_DEPENDS:=libcups_lede
PKG_FIXUP:=autoreconf

HOST_BUILD_DEPENDS:=python3/host
HOST_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/feeds/packages/lang/python/python3-version.mk

define Package/hplip_lede
#  SECTION:=utils
#  CATEGORY:=Utilities
  SECTION:=utils
  CATEGORY:=Printing
  SUBMENU:=Utilities
  TITLE:=(LEDE) HP Linux Imaging and Printing
  URL:=https://sourceforge.net/projects/hplip/
  DEPENDS+=+libjpeg +libusb-1.0 +libcups_lede +libcupsimage_lede
endef

define Package/hplip_lede/description
	HPLIP is an HP developed solution for printing, scanning, and faxing with HP inkjet and laser based printers in Linux.
endef

define Package/hplip-scan_lede
#  SECTION:=utils
#  CATEGORY:=Utilities
  SECTION:=utils
  CATEGORY:=Printing
  SUBMENU:=Utilities
  TITLE:=(LEDE) HP Linux Imaging and Printing - Scanning libraries
  URL:=https://sourceforge.net/projects/hplip/
  DEPENDS+=+libusb-1.0 +hplip_lede +libsane_lede
endef

define Package/hplip-scan_lede/description
	Libraries to use HP Printers/Scanners with Sane.
endef

define Package/hplip-ppds_lede
#  SECTION:=utils
#  CATEGORY:=Utilities
  SECTION:=utils
  CATEGORY:=Printing
  SUBMENU:=Utilities
  TITLE:=(LEDE) HP Linux Imaging and Printing - PPD files
  URL:=http://sourceforge.net/projects/hplip/
  DEPENDS+=+hplip_lede
endef

define Package/hplip-ppds_lede/description
	PPD files for CUPS.
endef

define Package/hplip-tools_lede
##  SECTION:=utils
##  CATEGORY:=Utilities
  SECTION:=utils
  CATEGORY:=Printing
  SUBMENU:=Utilities
  TITLE:=(LEDE) HP Linux Imaging and Printing - Command line tools
  URL:=http://sourceforge.net/projects/hplip/
  DEPENDS+=+hplip_lede +hplip-scan_lede
endef

define Package/hplip-tools_lede/description
	Command line tools for managing your printer.
endef

FS_LIB_DIR:=/usr/lib
FS_PYTHON3_SITE_DIR:=$(FS_LIB_DIR)/python$(PYTHON3_VERSION)/site-packages


TARGET_CFLAGS += \
	-I$(STAGING_DIR)/usr/include/python$(PYTHON3_VERSION)
	
TARGET_CXXFLAGS += \
	-I$(STAGING_DIR)/usr/include/python$(PYTHON3_VERSION)

CONFIGURE_VARS+= \
	PYTHON3_CFLAGS="-I$(STAGING_DIR)/usr/include/python$(PYTHON3_VERSION)" \
	PYTHON3_LDFLAGS="-lpython$(PYTHON3_VERSION)" \
	PYTHON3_LIB="python$(PYTHON3_VERSION)" \
	PYTHON3_LIBDIR="$(FS_LIB_DIR)" \
	PYTHON3_SITE_DIR="$(FS_PYTHON3_SITE_DIR)"

CONFIGURE_ARGS += \
	--disable-gui-build \
	--disable-network-build \
	--disable-fax-build \
	--disable-pp-build \
	--disable-doc-build \
	--disable-dbus-build \
	--disable-hpijs-only-build \
	--disable-hpcups-install \
	--disable-hpps-install \
	--disable-cups-drv-install \
	--enable-lite-build

define Build/Install
	mkdir -p $(PKG_INSTALL_DIR)/usr/share/sane
	sed -n -e '/key="usb.product_id"/{s/.*int_outof="0x//;s/;0x/\n/g;s/".*//;p}' \
		$(PKG_BUILD_DIR)/data/rules/20-hplip-devices.fdi | sort -u > \
		$(PKG_INSTALL_DIR)/usr/share/sane/03f0-hplip.usbid
endef

define Package/hplip_lede/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/.libs/libhpip.so* $(1)/usr/lib/
	$(CP) $(PKG_BUILD_DIR)/.libs/libhpmud.so* $(1)/usr/lib/

	$(INSTALL_DIR) $(1)/etc/hp
	$(CP) $(PKG_BUILD_DIR)/hplip.conf $(1)/etc/hp/hplip.conf
	
	$(INSTALL_DIR) $(1)/etc/udev/rules.d
	$(CP) $(PKG_BUILD_DIR)/data/rules/56-hpmud.rules $(1)/etc/udev/rules.d/

	$(INSTALL_DIR) $(1)/usr/share/hplip/data/models/
	$(CP) $(PKG_BUILD_DIR)/data/models/models.dat $(1)/usr/share/hplip/data/models/


	$(INSTALL_DIR) $(1)/usr/lib/cups/backend
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hp $(1)/usr/lib/cups/backend/
	
	$(INSTALL_DIR) $(1)/usr/lib/cups/filter
	#$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/cups/filter/* $(1)/usr/lib/cups/filter
	$(CP) $(PKG_BUILD_DIR)/prnt/hpijs/foomatic-rip-hplip  $(1)/usr/lib/cups/filter

	$(INSTALL_DIR) $(1)/usr/share/cups/drv/hp
	$(CP) $(PKG_BUILD_DIR)/prnt/drv/hpcups.drv $(1)/usr/share/cups/drv/hp/
	
	$(INSTALL_DIR) $(1)/usr/share/hal/fdi/preprobe/10osvendor
	$(CP) $(PKG_BUILD_DIR)/data/rules/20-hplip-devices.fdi $(1)/usr/share/hal/fdi/preprobe/10osvendor/
	
	$(INSTALL_DIR) $(1)/usr/share/ipp-usb/quirks
	$(CP) $(PKG_BUILD_DIR)/prnt/ipp-usb/HPLIP.conf $(1)/usr/share/hal/fdi/preprobe/10osvendor/
endef

define Package/hplip-scan_lede/install
	$(INSTALL_DIR) $(1)/usr/lib/sane
	$(CP) $(PKG_BUILD_DIR)/.libs/libsane-hpaio.so* $(1)/usr/lib/sane

	$(INSTALL_DIR) $(1)/etc/sane.d/dll.d/
	$(INSTALL_DATA) ./files/hplib.conf $(1)/etc/sane.d/dll.d/hplib

	$(INSTALL_DIR) $(1)/usr/share/sane
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/sane/03f0-hplip.usbid \
		$(1)/usr/share/sane/03f0-hplip.usbid
endef

define Package/hplip_lede/conffiles
/etc/hp/hplip.conf
endef

define Package/hplip-ppds_lede/install
	$(INSTALL_DIR) $(1)/usr/share/ppd/HP
	#$(CP) $(PKG_INSTALL_DIR)/usr/share/ppd/HP/* $(1)/usr/share/ppd/HP
	$(CP) $(PKG_BUILD_DIR)/ppd/hpcups/* $(1)/usr/share/ppd/HP
endef

define Package/hplip-tools_lede/install
	#$(INSTALL_DIR) $(1)/usr/lib/python$(PYTHON3_VERSION)/site-packages
	#$(CP) $(PKG_INSTALL_DIR)/usr/lib/python$(PYTHON3_VERSION)/site-packages/*.so $(1)/usr/lib/python$(PYTHON3_VERSION)/site-packages

	$(INSTALL_DIR) $(1)/usr/bin
	$(CP) $(PKG_BUILD_DIR)/hp $(1)/usr/bin

	$(INSTALL_DIR) $(1)/usr/share/hplip
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/*.py $(1)/usr/share/hplip
	$(CP) $(PKG_BUILD_DIR)/{base,copier,pcard,prnt,scan,check-plugin.py,hplip_clean.sh} $(1)/usr/share/hplip
	
	#$(INSTALL_DIR) $(1)/usr/lib/systemd/system
	#$(CP) $(PKG_INSTALL_DIR)/usr/lib/systemd/system/* $(1)/usr/lib/systemd/system/
endef

$(eval $(call BuildPackage,hplip_lede))
$(eval $(call BuildPackage,hplip-scan_lede))
$(eval $(call BuildPackage,hplip-ppds_lede))
$(eval $(call BuildPackage,hplip-tools_lede))

